package diamond.day02;

public class WindowsTest1 {
    public static void main(String[] args) {
        Window1 window1 = new Window1();
        Thread t1 = new Thread(window1);
        t1.setName("窗口一");
        Thread t2 = new Thread(window1);
        t2.setName("窗口二");
        Thread t3 = new Thread(window1);
        t3.setName("窗口三");
        t1.start();
        t2.start();
        t3.start();
    }
}

class Window1 implements Runnable{
    private int ticket =100;
//    Object o = new Object();
    public void run() {
        while (true){
            synchronized(this) {//此时的this：就是当前对象，就是Window1的对象 //synchronized(o) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":卖票，票号为：" + ticket);
                    ticket--;
                } else {
                    break;
                }
            }
        }
    }
}
